package com.jy.adscm.operation.aftersales.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.annotation.AdscmLogger;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.aftersales.bean.QueryShCustomerInfo;
import com.jy.adscm.operation.aftersales.bean.QueryShMachine;
import com.jy.adscm.operation.aftersales.bean.ShMachine;
import com.jy.adscm.operation.aftersales.service.ShCustomerInfoService;
import com.jy.adscm.operation.aftersales.service.ShMachineService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-07-03
 */
@RestController
@RequestMapping("/adscm/operation/aftersales/shMachine")
public class ShMachineController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    ShMachineService shMachineService;
    @AdscmLogger("新增机器信息")
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody ShMachine shMachine, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        shMachine.setCreator(token.getUser().getId().toString());
        shMachine.setCreateTime(new Date());
        boolean insert = shMachineService.insert(shMachine);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @AdscmLogger("修改机器信息")
    @RequestMapping("update")
    public  SimpleCoreResponse  update(@RequestBody ShMachine shMachine, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        shMachine.setCreator(token.getUser().getId().toString());
        shMachine.setCreateTime(new Date());
        boolean result = shMachineService.updateById(shMachine);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @AdscmLogger("删除机器信息")
    @RequestMapping("delete/{id}")
    public  SimpleCoreResponse  delete(@PathVariable Integer  id, HttpServletRequest request) throws AdscmSystemException {
        boolean result = shMachineService.deleteById(id);
        return  new SimpleCoreResponse("200","删除成功",result);
    }

    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll(@RequestBody QueryShMachine queryShMachine, HttpServletRequest request) throws  AdscmSystemException{
        Page<ShMachine> page =new Page<>(queryShMachine.getCurrentPage(),queryShMachine.getPageSize());
        page = shMachineService.selectAll(page,queryShMachine);
        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("selectByCid/{cid}")
    public  SimpleCoreResponse  selectByCid(@PathVariable Integer cid, HttpServletRequest request) throws  AdscmSystemException{
         List<ShMachine>  shMachines=shMachineService.selectByCid(cid);
        return  new SimpleCoreResponse("200","查询成功",shMachines);
    }
}

